<template>

  <el-dialog v-dialogDrag title="新增企业" :visible="isShowAddDialog" @close="dialogClose" :close-on-click-modal="false"
             width="60%">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="auto" class="demo-ruleForm">
      <el-form-item label="企业名称：" prop="companyName">
        <el-input type="text" v-model="ruleForm.companyName">
        </el-input>
      </el-form-item>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="企业简称：" prop="abbreviated">
            <el-input type="text" v-model="ruleForm.abbreviated">
            </el-input>
          </el-form-item>
          <el-form-item label="法人：" prop="corporate">
            <el-input type="text" v-model="ruleForm.corporate">
            </el-input>
          </el-form-item>
          <el-form-item label="办公电话：" prop="phone">
            <el-input type="text" v-model="ruleForm.phone">
            </el-input>
          </el-form-item>
          <el-form-item label="办公地址：" prop="address">
            <el-input type="text" v-model="ruleForm.address">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="信用代码：" prop="creditCode">
            <el-input type="text" v-model="ruleForm.creditCode">
            </el-input>
          </el-form-item>
          <el-form-item label="单位联系人：" prop="contact">
            <el-input type="text" v-model="ruleForm.contact">
            </el-input>
          </el-form-item>
          <el-form-item label="电子邮箱：" prop="email">
            <el-input type="text" v-model="ruleForm.email">
            </el-input>
          </el-form-item>
          <el-form-item label="微信号：" prop="wechat">
            <el-input type="text" v-model="ruleForm.wechat">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="企业类型：" prop="type">
        <!-- <el-radio v-if="!type" v-model="ruleForm.type" label="母公司"></el-radio> -->
        <div v-if="parent">
          <el-radio v-if="type == '空' || type == '子'" v-model="ruleForm.type" label="母公司"></el-radio>
          <el-radio v-if="type == '空'" v-model="ruleForm.type" label="子公司"></el-radio>
        </div>
        <div v-if="type == '母公司' || type == '子公司'">
          <el-radio v-if="type == '母公司'" v-model="ruleForm.type" label="子公司"></el-radio>
          <el-radio v-if="type == '母公司' || type == '子公司'" v-model="ruleForm.type" label="分公司"></el-radio>
        </div>
      </el-form-item>
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item label="公众号：" prop="officialAccount">
            <el-upload class="avatar-uploader" :action='actionAccount' list-type="picture-card"
                       :show-file-list="false" :on-success="accounthandleAvatarSuccess">
              <img v-if="accountUrl" :src="accountUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="公章：" prop="officialSeal">
            <el-upload class="avatar-uploader" :action='actionSeal' list-type="picture-card"
                       :show-file-list="false" :on-success="sealhandleAvatarSuccess">
              <img v-if="sealUrl" :src="sealUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="logo：" prop="logo">
            <el-upload class="avatar-uploader" :action='actionLogo' list-type="picture-card"
                       :show-file-list="false" :on-success="logohandleAvatarSuccess">
              <img v-if="logoUrl" :src="logoUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" @click="submitForm()" class="pull-right margin-l-25">确定
        </el-button>
        <el-button @click="dialogClose" class="pull-right">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import {add, correlation, correlationParent} from '@/api/systemManage/enterpriseMaage'

export default {
  name: 'enterpriseAdd',
  data() {
    return {
      // id: '',
      ruleForm: {
        type: '',
      },
      actionAccount: '/api/company/gzhUpload',
      actionLogo: '/api/company/logoUpload',
      actionSeal: '/api/company/officialSealUpload',
      accountUrl: '',
      logoUrl: '',
      sealUrl: '',
      rules: {
        companyName: [
          {required: true, message: '请输入企业名称', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱地址', trigger: 'blur'}
        ],
        abbreviated: [
          {required: true, message: '请输入企业简称', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '请输入公司地址', trigger: 'blur'}
        ],
        contact: [
          {required: true, message: '请输入单位联系人姓名', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入联系电话', trigger: 'blur'}
        ],
        type: [
          {required: true, message: '请选择公司类型', trigger: 'blur'}
        ]
      },
    };
  },
  props: {
    isShowAddDialog: {
      type: Boolean
    },
    parent: {
      type: Boolean
    },
    type: {
      type: String
    },
    father: {
      type: Number
    },
    qyid: {
      type: Number
    }
  },
  methods: {
    sealhandleAvatarSuccess(file) {
      this.sealUrl = file.data;
    },
    logohandleAvatarSuccess(file) {
      this.logoUrl = file.data;
    },
    accounthandleAvatarSuccess(file) {
      this.accountUrl = file.data;
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        this.ruleForm.logo = this.logoUrl;
        this.ruleForm.officialSeal = this.sealUrl;
        this.ruleForm.officialAccount = this.accountUrl;
        if (this.ruleForm.type == '母公司') {
          this.ruleForm.parentId = 0
        } else if (this.parent && this.ruleForm.type == '子公司') {
          this.ruleForm.parentId = 0
        } else {
          this.ruleForm.parentId = this.father
        }
        if (valid) {
          add(this.ruleForm).then((res) => {
            if (res) {
              if (this.parent) {
                if (this.ruleForm.type == '母公司') {
                  if (this.qyid == -1) {
                    let companyId = res.data.companyId
                    let userId = localStorage.getItem('userId')
                    let companyName = this.ruleForm.abbreviated
                    let type = 0
                    correlation({companyId, companyName, userId, type}).then((res) => {
                      localStorage.getItem('companyId', companyId)
                      this.$message.success('添加成功！')
                      this.$emit('Ok')
                      this.$emit('dialogClose')
                    })
                  } else {

                    let parentId = res.data.companyId
                    let childrenId = this.qyid
                    correlationParent({parentId, childrenId}).then((res) => {
                      let companyId = parentId
                      let userId = localStorage.getItem('userId')
                      let companyName = this.ruleForm.abbreviated
                      let type = 0
                      correlation({companyId, companyName, userId, type}).then((res) => {
                        localStorage.getItem('companyId', companyId)
                        this.$message.success('添加成功！')
                        this.$emit('Ok')
                        this.$emit('dialogClose')
                        // this.golist()
                      })
                    })
                  }
                } else if (this.ruleForm.type == '子公司') {
                  let obj = {
                    companyId: res.data.companyId,
                    userId: localStorage.getItem('userId'),
                    type: 0,
                    companyName: this.ruleForm.companyName
                  }
                  correlation(obj).then((res) => {
                    localStorage.getItem('companyId', companyId)
                    this.$message.success('添加成功！')
                    this.$emit('Ok')
                    this.$emit('dialogClose')
                  })
                }
              }
              this.$emit('Ok')
            }
          })
        } else {
          return
        }
      });
    },

    dialogClose() {
      this.$emit('dialogClose')
    }
  },
  mounted() {
    if (this.type == '子公司') {
      this.ruleForm.type = '分公司'
    } else if (this.type == '子') {
      this.ruleForm.type = '母公司'
    }
  }

}
</script>
<style lang="less" scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
}

.avatar {
  width: 146px;
  height: 146px;
  object-fit: contain;
  display: block;
}
</style>
